__author__ = 'Paul'
from zope.component import queryUtility

from plone.registry.interfaces import IRegistry

from Acquisition import aq_inner

from Products.CMFCore.utils import getToolByName

from plone.app.discussion.interfaces import IDiscussionSettings


class ConversationView(object):
    """Sobreescribe el metodo para habilitarlo en los tipos de contenido de la BP"""
    def enabled(self):


        context = aq_inner(self.context)
        # Fetch discussion registry
        registry = queryUtility(IRegistry)
        settings = registry.forInterface(IDiscussionSettings, check=False)

        # Check if discussion is allowed globally
        if not settings.globally_enabled:
            return False

        # Check if discussion is allowed on the content object
        if context.allow_discussion is not None:
            return context.allow_discussion

        # Check if discussion is allowed on the content type
        portal_types = getToolByName(self, 'portal_types')
        document_fti = getattr(portal_types, context.portal_type)
        if context.portal_type=="Products.bpContents.articulo":
            return True
        return document_fti.getProperty('allow_discussion')